<?php

class Page extends CActiveRecord
{
	/**
	 * The followings are the available columns in table 'tbl_comment':
	 * @var integer $id
	 * @var string $content
	 * @var integer $status
	 * @var integer $created
	 * @var string $author
	 * @var string $email
	 * @var string $url
	 * @var integer $post_id
	 */
	const STATUS_PENDING=1;
	const STATUS_APPROVED=2;

	/**
	 * Returns the static model of the specified AR class.
	 * @return CActiveRecord the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{page}}';
	}

	/**
	 * @return array validation rules for model attributes.

	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			//array('content, author, email', 'required'),
			//array('author, email, url', 'length', 'max'=>128),
			//array('email','email'),
			//array('url','url'),
		);
	}
	 */
	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'comments' => array(self::HAS_MANY, 'Comment', 'page'),
			'commentCount' => array(self::STAT, 'Comment', 'page'), //'condition'=>'status='.Comment::STATUS_APPROVED),
		);
	}
	public function taggedPages()
	{
		$now=new CDbExpression("NOW()");
		$tags=Yii::app()->db->createCommand()
    		->select('p.id as id, p.name as name, p.url as url, t.name as tag' )
    		->from('{{pagetag}} pt')
    		->join('{{tag}} t', 't.id=pt.tag')
    		->join('{{page}} p', 'p.id=pt.page and p.startdate <='.$now.' and p.enddate >'.$now)
    		->where('pt.tag in (select tag from {{pagetag}} where page = '.$this->id.') and pt.page != '.$this->id)
    		->order('p.name')
    		->queryall();
    	return $tags;
	}
	
	public function getPageStructure($site)
	{
		$sitePages=array();
		$lvl=0;
		$items=$this->getPages($site, 0);
		foreach($items as $item)
		{
			array_push($sitePages, array('name'=> $item['name'], 'url'=>$item['url'], 'lvl'=>$lvl));
			$sitePages=$this->getParentPages($site, $item['id'], $sitePages, $lvl+1);
		}
    	return $sitePages;
	}
	
	public function getPages($site, $parentPage)
	{
		$now=new CDbExpression("NOW()");
		$items=Yii::app()->db->createCommand()
    		->select('p.id as id, p.name as name, p.show as show, p.url as url, p.parent as parent' )
    		->from('{{page}} p')
    		->join('{{site_pages}} sp', 'p.id=sp.page')
    		->where('sp.site='.$site.' and p.startdate <='.$now.' and p.enddate >'.$now.' and parent='.$parentPage)
    		->order('sp.pageorder')
    		->queryall();	
    	return $items;
	}
	public function getParentPages($site, $parentPage, $sitePages, $lvl=0)
	{
		$items=$this->getPages($site, $parentPage);
		foreach($items as $item)
		{
			array_push($sitePages, array('name'=> $item['name'], 'url'=>$item['url'],'lvl'=>$lvl));
		}
    	return $sitePages;
	}

	public function getRobots($url)
	{
		if (strlen($url) <=10) $urlPage = '/';
		else $urlPage = substr($url,10);
		$robots = 'all';
		$items=Yii::app()->db->createCommand()
    		->select('robots, description' )
    		->from('{{page}}')
    		->where("url ='".$urlPage."'")
    		->queryall();
    		if ($items[0]['robots'] == 0) $robots = 'noindex,nofollow';
    		// contentTitle wird in META-Tags für FB and Google verwendet
    		if ($items[0]['title'] == null) $contentTitle = $items[0]['name'];
    		else $contentTitle = $items[0]['title'];
    		
		return array(	'robots'=>$robots, 
						'description'=>$items[0]['description'], 
						'contentTitle'=>$contentTitle
						);
	}	
	public function getMenu($site)
	{
		$now=new CDbExpression("NOW()");
		$items=Yii::app()->db->createCommand()
    		->select('p.id as id, p.name as name, p.show as show, p.url as url, p.visible as visible' )
    		->from('{{page}} p')
    		->join('{{site_pages}} sp', 'p.id=sp.page')
    		->where('sp.site='.$site.' and p.startdate <='.$now.' and p.enddate >'.$now)
    		->order('sp.pageorder')
    		->queryall();
		$menuItems=array();
		foreach($items as $item)
		{
			if($item['visible']=== 'isGuest')
				array_push($menuItems, array('label'=> $item['show'], 'url'=>array($item['url']), 'visible'=>Yii::app()->user->isGuest));
			elseif($item['visible']=== 'isNotGuest')
				array_push($menuItems, array('label'=> $item['show'], 'url'=>array($item['url']), 'visible'=>!Yii::app()->user->isGuest));
			else
				array_push($menuItems, array('label'=> $item['show'], 'url'=>array($item['url'])));
		}
		
    	return $menuItems;
	}

	/**
	 * @return array customized attribute labels (name=>label)

	public function attributeLabels()
	{
		return array(
			'id' => 'Id',
			'content' => 'Comment',
			'status' => 'Status',
			'create_time' => 'Create Time',
			'author' => 'Name',
			'email' => 'Email',
			'url' => 'Website',
			'page' => 'Page',
		);
	}
	 */


	/**
	 * @param Post the post that this comment belongs to. If null, the method
	 * will query for the post.
	 * @return string the permalink URL for this comment

	public function getUrl($post=null)
	{
		if($post===null)
			$post=$this->post;
		return $post->url.'#c'.$this->id;
	}
	 */
	public function addComment($comment)
	{
		$comment->user=Yii::app()->user->id; 
		$comment->page=$this->id;
		return $comment->save();
	}
}